//
//  MainViewController.swift
//  Sina
//
//  Created by 邹志华 on 15/10/28.
//  Copyright © 2015年 zzh. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController{

    func composeButtonClick() {
       //__FUNCTION__ 用来打印方法的名称
        print(__FUNCTION__)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
//        let newTabBar = MainTabBar()
//        
//        newTabBar.composeButton.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        //使用KVC

//        setValue(newTabBar, forKey: "tabBar")
        
        
        tabBar.tintColor = UIColor.orangeColor()
        //        //首页
//        let homeVC = HomeTableViewController()
//        homeVC.title = "首页" //首页名称
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home") //首页图标
//          homeVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
//
//        let selImg = UIImage(named: "tabbar_home_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        homeVC.tabBarItem.selectedImage = selImg
//
//        addChildViewController(UINavigationController(rootViewController: homeVC))

        let homeVC       = HomeTableViewController()
        addChildViewController(homeVC,title: "首页",imageNmage: "tabbar_home")


        //消息
//        let message = MessageTableViewController()
//        message.title = "消息" //首页名称
//        message.tabBarItem.image = UIImage(named: "tabbar_message_center") //首页图标
//        addChildViewController(UINavigationController(rootViewController: message))

        let message      = MessageTableViewController()
        addChildViewController(message,title: "消息",imageNmage: "tabbar_message_center")

        
        //添加加号按钮
        let controller = UIViewController()
        self.addChildViewController(controller,title: "",imageNmage: "zzh")
        

        //发现
//        let discove = DiscoverTableViewController()
//        discove.title = "发现" //
//        discove.tabBarItem.image = UIImage(named: "tabbar_discover") //
//        addChildViewController(UINavigationController(rootViewController: discove))
//

        let discove      = DiscoverTableViewController()
        addChildViewController(discove,title: "发现",imageNmage: "tabbar_discover")

//        let profile = ProfileTableViewController()
//        profile.title = "我" //首页名称
//        profile.tabBarItem.image = UIImage(named: "tabbar_profile") //首页图标
//        addChildViewController(UINavigationController(rootViewController: profile))
//

        let profile      = ProfileTableViewController()
        addChildViewController(profile,title: "我",imageNmage: "tabbar_profile")
        
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        let width = tabBar.bounds.width / CGFloat(5)
        composeButton.frame = CGRect(x: width * 2 , y: 0, width: width, height: tabBar.bounds.height)
        tabBar.addSubview(composeButton)
    }
    
    
    
    /**
     添加子控制器
     
     - parameter controller: 控制器
     - parameter title:      标题
     - parameter imageNmage: 图片名称
     */
    func addChildViewController(controller: UIViewController,title:String,imageNmage:String
        ) {
        controller.title = title
            controller.tabBarItem.image = UIImage(named: imageNmage)
            addChildViewController(UINavigationController(rootViewController: controller))
    }

    
    //        MARK: -懒加载
    /// 撰写按钮
    lazy var composeButton : UIButton = {
        
        
        let button = UIButton()
        
        //加号按钮图片
        button.setImage(UIImage (named:"tabbar_compose_icon_add"), forState:UIControlState.Normal)
        button.setImage(UIImage (named:"tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)
        
        //加号按钮背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button") , forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted") , forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //        self.addSubview(button)

        return button
    }()

}
